package com.d3works.apps.businessproduction.service;

import com.d3works.apps.base.service.PriceListService;
import com.d3works.apps.base.service.ProductCompanyService;
import com.d3works.apps.businessproject.service.TimesheetProjectServiceImpl;
import com.d3works.apps.hr.db.Timesheet;
import com.d3works.apps.hr.db.repo.TimesheetLineRepository;
import com.d3works.apps.hr.db.repo.TimesheetRepository;
import com.d3works.apps.hr.service.app.AppHumanResourceService;
import com.d3works.apps.hr.service.config.HRConfigService;
import com.d3works.apps.hr.service.timesheet.TimesheetLineService;
import com.d3works.apps.hr.service.user.UserHrService;
import com.d3works.apps.message.service.TemplateMessageService;
import com.d3works.apps.production.service.app.AppProductionService;
import com.d3works.apps.project.db.repo.ProjectPlanningTimeRepository;
import com.d3works.apps.project.db.repo.ProjectRepository;
import com.d3works.apps.project.db.repo.ProjectTaskRepository;
import com.d3works.apps.project.service.ProjectService;
import com.d3works.auth.db.repo.UserRepository;
import com.d3works.exception.D3worksException;
import com.d3works.inject.Beans;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

public class TimesheetBusinessProductionServiceImpl extends TimesheetProjectServiceImpl {

  @Inject
  public TimesheetBusinessProductionServiceImpl(
      PriceListService priceListService,
      AppHumanResourceService appHumanResourceService,
      HRConfigService hrConfigService,
      TemplateMessageService templateMessageService,
      ProjectRepository projectRepo,
      UserRepository userRepo,
      UserHrService userHrService,
      TimesheetLineService timesheetLineService,
      ProjectPlanningTimeRepository projectPlanningTimeRepository,
      ProjectTaskRepository projectTaskRepo,
      ProductCompanyService productCompanyService,
      TimesheetLineRepository timesheetlineRepo,
      TimesheetRepository timeSheetRepository,
      ProjectService projectService) {
    super(
        priceListService,
        appHumanResourceService,
        hrConfigService,
        templateMessageService,
        projectRepo,
        userRepo,
        userHrService,
        timesheetLineService,
        projectPlanningTimeRepository,
        projectTaskRepo,
        productCompanyService,
        timesheetlineRepo,
        timeSheetRepository,
        projectService);
  }

  @Override
  @Transactional(rollbackOn = {Exception.class})
  public void confirm(Timesheet timesheet) throws D3worksException {
    super.confirm(timesheet);
    AppProductionService appProductionService = Beans.get(AppProductionService.class);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getManageBusinessProduction()) {
      Beans.get(OperationOrderTimesheetServiceImpl.class)
          .updateAllRealDuration(timesheet.getTimesheetLineList());
    }
  }

  @Override
  @Transactional
  public void validate(Timesheet timesheet) {
    super.validate(timesheet);
    AppProductionService appProductionService = Beans.get(AppProductionService.class);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getManageBusinessProduction()) {
      Beans.get(OperationOrderTimesheetServiceImpl.class)
          .updateAllRealDuration(timesheet.getTimesheetLineList());
    }
  }

  @Override
  @Transactional
  public void refuse(Timesheet timesheet) {
    super.refuse(timesheet);
    AppProductionService appProductionService = Beans.get(AppProductionService.class);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getManageBusinessProduction()) {
      Beans.get(OperationOrderTimesheetServiceImpl.class)
          .updateAllRealDuration(timesheet.getTimesheetLineList());
    }
  }

  @Override
  @Transactional
  public void cancel(Timesheet timesheet) {
    super.cancel(timesheet);
    AppProductionService appProductionService = Beans.get(AppProductionService.class);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getManageBusinessProduction()) {
      Beans.get(OperationOrderTimesheetServiceImpl.class)
          .updateAllRealDuration(timesheet.getTimesheetLineList());
    }
  }

  @Override
  @Transactional
  public void draft(Timesheet timesheet) {
    super.draft(timesheet);
    AppProductionService appProductionService = Beans.get(AppProductionService.class);

    if (appProductionService.isApp("production")
        && appProductionService.getAppProduction().getManageBusinessProduction()) {
      Beans.get(OperationOrderTimesheetServiceImpl.class)
          .updateAllRealDuration(timesheet.getTimesheetLineList());
    }
  }
}
